package com.gmrz.uaf.remote.protocol.v1.processor;

import com.gmrz.uaf.common.Constants;
import com.gmrz.uaf.common.UAFException;
import com.gmrz.uaf.db.DAOException;
import com.gmrz.uaf.db.UAFDAOFactory;
import com.gmrz.uaf.db.UAFDBConnectionMgr;
import com.gmrz.uaf.db.dao.AuthenticatorDAO;
import com.gmrz.uaf.protocol.v1.processor.UAFBaseProcessor;
import com.gmrz.uaf.protocol.v1.processor.exception.UAFErrorCode;
import com.gmrz.uaf.protocol.v1.schema.*;
import com.gmrz.uaf.remote.plugin.AuthServicePluginManager;
import com.gmrz.util.Convert;
import com.gmrz.util.UUIDGenerator;
import com.gmrz.util.ZXingCode;
import com.gmrz.util.db.DBUtil;
import com.google.inject.Inject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.util.Date;

/**
 * Created by gongyuqiang on 2017/5/8.
 */
public class UAPOOBGenerateProcessor extends UAFBaseProcessor {
    private static final Logger LOG = LogManager.getLogger(UAPOOBGenerateProcessor.class);

    AuthServicePluginManager serviceManager;

    @Inject
    public void setServiceManager(AuthServicePluginManager serviceManager) {
        this.serviceManager = serviceManager;
    }

    /**
     * @param scanData
     * @param isGen
     * @return
     * @throws UAFException
     */
    public OOBGenerate oobGenerate(String scanData, boolean isGen,String appID,String userName) throws UAFException {
        try {
            // 生成认证令牌
            byte[] authToken = getRandom(32);
            String authTokenBase64 = Convert.toBase64(authToken);

            OOBInfo oobInfo = new OOBInfo();
            oobInfo.setScanData(scanData);
            oobInfo.setStatus(Constants.OOBStatus.NONE_SCANCODE.getStatus());

            Connection conn = UAFDBConnectionMgr.getConnection();
            try {
                AuthenticatorDAO adao = UAFDAOFactory.createAuthenticatorDAO(conn);
                OOBCodeBean oobCodeBean = new OOBCodeBean();
                oobCodeBean.setOobCodeID(UUIDGenerator.generateID());
                oobCodeBean.setAuthToken(authTokenBase64);
                oobCodeBean.setScanData(scanData);
                oobCodeBean.setAppID(appID);
                oobCodeBean.setCustNO(userName);
                oobCodeBean.setCodeStatus(Constants.OOBStatus.NONE_SCANCODE.getStatus());
                adao.insertOOBCode(oobCodeBean);

            }catch (DAOException e1){
                LOG.error("insert oob code error",e1);
            }finally {
                DBUtil.close(conn);
            }
            // 放入缓存，并设置过期时间
            this.cacheOOBInfo(oobInfo, authTokenBase64);

            OOBGenerate oobGenerate = new OOBGenerate();
            oobGenerate.setAuthToken(authTokenBase64);
            if (isGen) {
                // 生成二维码
                byte[] image = ZXingCode.drawLogoQRCode(null, authTokenBase64, null);
                oobGenerate.setQrcode(Convert.toBase64String(image));
            }
            // 返回带有认证口令、生成的二维码数据
            return oobGenerate;
        } catch (Exception e) {
            LOG.error("oobGenerate error", e);
            throw new UAFException(UAFErrorCode.PROTOCOL_GENERATE_AUTHTOKEN);
        }
    }


}
